
const { ARTICLE } = require('../../../config/article.js');
const { testMobile } = require('../../../utils/util.js');
const { mayjorList, schoolList, rulesList, addressList } = require('../../../config/api/index.js');
const { pingce } = require('../../../config/api/form.js');
var app = getApp();
let self
Page({

  /**
   * 页面的初始数据
   */
  data: {
		inputName: '请输入学生姓名',  // 报名姓名
		inputPhone: '请输入联系电话',  // 报名电话
		inputMajor: '',  // 报名专业
		inputSchool: '',  // 报名学校
		inputAges: '',  //  年龄
		inputEdu: '',  // 学历
		inputEngLevel: '',  //  英语水平  
		inputTrade: '',  //  所处行业
		inputsex: 1,  // 性别
		schoolName: '',  // 学校名称
		majorName: '',  // 专业名称
		sex:[
			{ id: 0, value: '男' },
			{ id: 1, value: '女', checked: 'true' },
		],	
		ages:[],
		edu: [],  // 学历
		major: [{id: -2, name:'请选择专业',pid:'',level:''}],  // 专业列表
		school: [{ id: -2, name: '请选择学校', pid: '', level: '' }],  // 学校列表
		other: [{ id: -1, name: '其他', pid: '', level: '' }],  // 其他
		englishLevel: [],  // 英语水平
		trade: [],  // 行业列表
		indexAges: 0,  // 当前年龄下标
		indexEdu: 0,  // 当前学历下标 
		indexMajor: 0,  // 当前专业下标
		indexSchool: 0,  // 当前学校下标
		indexEnglishLevel: 0,  // 当前英语水平下标
		indexTrade: 0,  // 当前行业下标
		submitStast: false,  // 提交状态判断
		filterPage: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		self = this;
		self.setData({
			ages: ARTICLE['ages'].data,
			edu: ARTICLE['edu'].data,
			englishLevel: ARTICLE['englishLevel'].data,  // 英语水平
			trade: ARTICLE['trade'].data,  // 行业列表 
		})
		mayjorList({
			data: { pid: 419, per_page: 20},
			success (res) {
				let major = self.data.major.concat(res.data.items) 
				self.setData({
					major
				})
			}	
		})
		self.getSchoolFilter()
		
  },
	getSchoolFilter(params) {  // 获取全部学校列表
		const { filterPage, school, other } = self.data;
		return schoolList({
			data: {
				...{
					per_page: 100,
					is_get: 1
				},
				...params
			},
			success(res) {

				let totalPages = res.data.total_pages
				let resLength = res.data.total_items
				if (filterPage <= totalPages && resLength > 100) {
					self.setData({
						filterPage: filterPage + 1,
						school: [...school, ...res.data.items]
					})
					setTimeout(function () {
						self.getSchoolFilter({ page: filterPage + 1 })
					}, 0)
				} else {
					self.setData({
						school: [...school, ...res.data.items, ...other]
					})
				}
			}
			
		})
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
	inputName: function (e) {
		self.setData({
			inputName: e.detail.value
		})
	},
	inputNameBlur: function (e) {
		self.setData({
			inputName: e.detail.value || '请输入学生姓名',
			submitStast: e.detail.value ? true : false
		})
	},
	inputPhoneBlur: function (e) {
		self.setData({
			inputPhone: e.detail.value || '请输入联系电话',
			submitStast: e.detail.value ? true : false
		})
	},
	inputPhone: function (e) {
		self.setData({
			inputPhone: e.detail.value
		})
	},
	sexChange(e) {
		this.setData({
			inputsex: e.detail.value || 1
		})
	},
	ageChange(e){
		this.setData({
			indexAges: e.detail.value,
			inputAges: self.data.ages[e.detail.value].id
		})
	},
	eduChange(e){
		this.setData({
			indexEdu: e.detail.value,
			inputEdu: self.data.edu[e.detail.value].id
		})
	},
	majorChange: function (e) {
		this.setData({
			indexMajor: e.detail.value,
			inputMajor: self.data.major[e.detail.value].id,
		})
	},
	
	schoolChange: function (e) {
		self.setData({
			indexSchool: e.detail.value,
			inputSchool: self.data.school[e.detail.value].id,
		})
	},
	englishLevelChange: function (e) {
		this.setData({
			indexEnglishLevel: e.detail.value,
			inputEngLevel: self.data.englishLevel[e.detail.value].id
		})
	},
	tradeChange: function (e) {
		self.setData({
			indexTrade: e.detail.value,
			inputTrade: self.data.trade[e.detail.value].id
		})
	},
	onSubmit: function () {
		const { inputName, inputPhone, inputMajor, inputEdu, inputAges, inputSchool, inputEngLevel, inputTrade, inputsex, submitStast, school, indexSchool, major, indexMajor } = self.data;
		
		if (!submitStast && inputName == '请输入学生姓名') {
			wx.showToast({
				title: '姓名不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!testMobile(inputPhone)) {
			wx.showToast({
				title: '手机号码错误',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!inputMajor || inputMajor == -2) {
			wx.showToast({
				title: '专业不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!inputSchool || inputSchool == -2) {
			wx.showToast({
				title: '学校不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!inputEdu || inputEdu == -2) {
			wx.showToast({
				title: '学历不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!submitStast) {
			wx.showToast({
				title: '隐私保障为勾选',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		let formInfo = {
			age: parseInt(inputAges) == -2 ? '' : parseInt(inputAges),
			project: parseInt(inputMajor) || '',
			school: parseInt(inputSchool) || '',
			sex: parseInt(inputsex) || 1,
			edu: parseInt(inputEdu) == -2 ? '' : parseInt(inputEdu),
			en_level: parseInt(inputEngLevel) == -2 ? '' : parseInt(inputEngLevel),
			company: parseInt(inputTrade) == -2 ? '' : parseInt(inputTrade),
			custom: inputName || '',
			phone: inputPhone || '',
			content: school[indexSchool]['name'] + '-' + major[indexMajor]['name'] + '-报考测评',
			subsite_id: 52
		}
		pingce({
			data: formInfo,
			success(res) {
				if (res.errCode == 200) {
					let text = res.data.school || '其他学校'
					if (res.data.result == 100){
						wx.showModal({
							title: '恭喜！',
							content: '您满足 “' + text + '” 报考条件，老师会尽快联系您',
							cancelText: '重新测试',
							success(res) {
								if (res.confirm) {
									wx.reLaunch({
										url: '/pages/test-page/test-page'
									})
								} else {
									self.setData({
										inputMajor: '',
										inputSchool: '',
										indexMajor: 0,  // 当前专业下标
										indexSchool: 0,  // 当前学校下标
									})
								}
							},
							fail(e) {
								console.log('请求失败')
							}
						})
					} else{
						wx.showModal({
							title: '很遗憾',
							content: '您未满足 “' + text + '” 报考条件',
							cancelText: '重新测试',
							success(res) {
								if (res.confirm) {
									wx.reLaunch({
										url: '/pages/test-page/test-page'
									})
								} else {
									self.setData({
										inputMajor: '',
										inputSchool: '',
										indexMajor: 0,  // 当前专业下标
										indexSchool: 0,  // 当前学校下标
									})
								}
							},
							fail(e) {
								console.log('请求失败')
							}
						})
					}

				} else {
					wx.showModal({
						title: '请求失败！',
						content: '服务器繁忙，稍后尝试!',
					});
				}
			}
		})

	
			
	},
	checkboxChange: function (e) {
		self.setData({
			submitStast: e.detail.value == "1" ? true : false,
		})
	},
	guaranteePage: function (){
		wx.navigateTo({
			url: '/pages/guarantee/guarantee',
		})
	},
	onShareAppMessage: function (res) {
		return {
			title: '在职研究生网报考测评',
			path: '/pages/form-page/test/test'
		}
	}
})